home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xpm / pixmap / TextOptions.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  10KB  |  345 lines

  1. /*
  2.  * $Id: TextOptions.c,v 1.4 1992/10/27 08:42:44 mallet Exp $
  3.  *
  4.  * Copyright 1992 Lionel Mallet
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appears in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of Lionel MALLET not be used in
  11.  * advertising or publicity pertaining to distribution of the software
  12.  * without specific, written prior permission.  Lionel MALLET makes no
  13.  * representations about the suitability of this software for any
  14.  * purpose.  It is provided "as is" without express or implied warranty.
  15.  *
  16.  * Lionel MALLET DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  17.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18.  * FITNESS, IN NO EVENT SHALL Lionel MALLET BE LIABLE FOR ANY SPECIAL,
  19.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  20.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  21.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  22.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  *
  24.  *  This software is opened and free. Furthermore, everybody is kindly
  25.  * invited to participate to improve it for the benefit of all.
  26.  * Improvements can be new features, bugs fixes and porting issues
  27.  * resolution.
  28.  *
  29.  * Author:  Tim Wise - Scientific & Engineering Software (SES), Inc.
  30.  */
  31.  
  32.  
  33. extern void unsetKillfromWM();
  34.  
  35.  
  36. static void PopupTextOptions(button, textOptions)
  37.     Widget      button;
  38.     TextOptions *textOptions;
  39. {
  40.     Position       x, y;
  41.     Dimension       width, height;
  42.  
  43.     if (!textOptions->up) {
  44.  
  45.         XtVaGetValues( button, 
  46.                        XtNwidth,  &width,
  47.                        XtNheight, &height,
  48.                        NULL );
  49.         
  50.         XtTranslateCoords(button, 
  51.               (Position) (width / 2), (Position) (height / 2),
  52.               &x, &y );
  53.     
  54.         XtVaSetValues( textOptions->shell, 
  55.                        XtNx, x,
  56.                        XtNy, y,
  57.                        NULL );
  58.  
  59. #ifndef USE_ATHENA
  60.     XtManageChild(textOptions->popup);
  61. #else
  62.     XtPopup(textOptions->popup, XtGrabNone);
  63. #endif
  64.     unsetKillfromWM(textOptions->popup);
  65.     textOptions->up = True;
  66.     } else textOptions->up = False;
  67.  
  68. }
  69.  
  70.  
  71. static void PopdownTextOptions( w, client_data, call_data )
  72.     Widget    w;        /* button of form of popup */
  73.     XtPointer    client_data;    /* TextOptions* */
  74.     XtPointer    call_data;    /* unused */
  75. {
  76.     TextOptions *text_options = (TextOptions *) client_data;
  77. #ifndef USE_ATHENA
  78.     XtUnmanageChild(textOptions->popup);
  79. #else
  80.     XtPopdown(textOptions->popup);
  81. #endif
  82. }
  83.  
  84.  
  85. static void Apply( w, client_data, call_data )
  86.     Widget    w;        /* apply button */
  87.     XtPointer    client_data;    /* TextOptions* */
  88.     XtPointer    call_data;    /* unused */
  89. {
  90.     TextOptions *text_options = (TextOptions *) client_data;
  91.     String    new_font_name = NULL;
  92.     String    new_text_string = NULL;
  93.     
  94.     /* get new font name and new text string */
  95. #ifndef USE_ATHENA
  96.     /* Big hack for Motif: avoid arbitrary resizing problems */
  97.     Dimension width, height;
  98.     XtVaGetValues(textOptions->popup, XmNwidth, &width, XmNheight, &height,
  99.           NULL);
  100.     
  101.     new_font_name = (String) XmTextFieldGetString(text_options->font);
  102.     new_text_string = (String) XmTextFieldGetString(text_options->text);
  103.     
  104. #else
  105.     XtVaGetValues( text_options->font, 
  106.                    XtNstring, &new_font_name, 
  107.                    NULL );
  108.     new_font_name = XtNewString( new_font_name );
  109.  
  110.     XtVaGetValues( text_options->text, 
  111.                    XtNstring, &new_text_string, 
  112.                    NULL );
  113.     new_text_string = XtNewString( new_text_string );
  114. #endif
  115.  
  116.     /* if new font name != current font name, ... */
  117.  
  118.     if (strcmp( new_font_name, text_options->font_name )) {
  119.  
  120.         Display     *dpy      = XtDisplay(text_options->popup);
  121.         XFontStruct *new_font = XLoadQueryFont( dpy, new_font_name );
  122.  
  123.         if (new_font) {
  124. #ifndef USE_ATHENA
  125.         XmString xmstr;
  126.  
  127.         xmstr = XmStringCreateLtoR("Valid font, text ready to paste",
  128.                        XmSTRING_DEFAULT_CHARSET);
  129.         XtVaSetValues(text_options->status, XmNlabelString, xmstr, NULL);
  130.         XmStringFree(xmstr);
  131.         
  132.         /* Big hack for Motif: avoid arbitrary resizing problems */
  133.         XtVaSetValues(textOptions->shell, XmNwidth, width,
  134.               XmNheight, height, NULL);
  135. #else    
  136.             XtVaSetValues(text_options->status, 
  137.               XtNlabel, "Valid font, text ready to paste",
  138.               NULL );
  139. #endif
  140.         }
  141.         else {
  142. #ifndef USE_ATHENA
  143.         XmString xmstr;
  144.  
  145.         xmstr = XmStringCreateLtoR("No font with this name!",
  146.                        XmSTRING_DEFAULT_CHARSET);
  147.         XtVaSetValues(text_options->status, XmNlabelString, xmstr, NULL);
  148.         XmStringFree(xmstr);
  149.         
  150.         /* Big hack for Motif: avoid arbitrary resizing problems */
  151.         XtVaSetValues(textOptions->shell, XmNwidth, width,
  152.               XmNheight, height, NULL);
  153. #else    
  154.             XtVaSetValues( text_options->status, 
  155.                            XtNlabel, "No font with this name!", 
  156.                            NULL );
  157. #endif
  158.         }
  159.  
  160.         if (text_options->font_struct)
  161.             XFreeFont( dpy, text_options->font_struct );
  162.         XtFree( text_options->font_name );
  163.  
  164.         text_options->font_struct = new_font;
  165.         text_options->font_name   = XtNewString( new_font_name );
  166.  
  167.         PWSetFont( pixmap_widget, text_options->font_struct );
  168.     }
  169.  
  170.     /* if new text string != current text string, ... */
  171.     if (strcmp( new_text_string, text_options->text_string ) != 0) {
  172.  
  173.         XtFree( text_options->text_string );
  174.         text_options->text_string = XtNewString( new_text_string );
  175.         PWSetText( pixmap_widget, text_options->text_string );
  176.     }
  177.  
  178. #ifndef USE_ATHENA
  179. #endif
  180.  
  181.     XtFree( new_font_name );
  182.     XtFree( new_text_string );
  183. }
  184.  
  185. #ifdef USE_ATHENA
  186. static void ApplyAction( w, event, params, num_params )
  187.     Widget    w;        /* either text or font widget */
  188.     XEvent    *event;
  189.     String    *params;
  190.     Cardinal    *num_params;
  191. {
  192.     Widget    form = XtParent(w);
  193.  
  194.     /* how to get TextOptions data structure ? */
  195.     /* oh, just use that global pointer */
  196.     Apply( w, (XtPointer) textOptions, (XtPointer) NULL );
  197. }
  198.  
  199.  
  200. static XtActionsRec actions[] = {
  201.   { "apply", ApplyAction }
  202. };
  203. #endif
  204.  
  205. TextOptions* CreateTextOptions(parent, name ) 
  206.     Widget    parent;
  207.     String    name;
  208. {
  209.     Widget    popup, shell, textOptions, 
  210.         title, 
  211.         status, 
  212.         font_label, font, 
  213.         text_label, text, 
  214.         close, apply;
  215.     String    font_name, text_string;
  216.     
  217.     TextOptions *text_options = (TextOptions *)XtCalloc(1,
  218.                             sizeof(TextOptions));
  219.  
  220. #ifdef USE_ATHENA
  221.     XtAppAddActions(pixmap_context, actions, XtNumber(actions));
  222. #endif
  223.  
  224. #ifndef USE_ATHENA
  225.     textOptions = XmCreateFormDialog(parent, name, NULL, 0);
  226.     popup = textOptions;
  227.     shell = XtParent(popup);
  228.     
  229.     title = XmCreateLabelGadget(textOptions, "title", NULL, 0);
  230.     XtManageChild(title);
  231.  
  232.     status = XmCreateLabelGadget(textOptions, "fontStatus", NULL, 0);
  233.     XtManageChild(status);
  234.  
  235.     font_label = XmCreateLabelGadget(textOptions, "fontLabel", NULL, 0);
  236.     XtManageChild(font_label);
  237.  
  238.     font = (Widget) XmCreateTextField(textOptions, "fontName", NULL, 0);
  239.     XtManageChild(font);
  240.     XtAddCallback(font, XmNactivateCallback, Apply, (XtPointer)text_options);
  241.  
  242.     text_label = XmCreateLabelGadget(textOptions, "textLabel", NULL, 0);
  243.     XtManageChild(text_label);
  244.     
  245.     text = (Widget) XmCreateTextField(textOptions, "textString", NULL, 0);
  246.     XtManageChild(text);
  247.     XtAddCallback(text, XmNactivateCallback, Apply, (XtPointer)text_options);
  248.     
  249.     close = XmCreatePushButtonGadget(textOptions, "close", NULL, 0);
  250.     XtManageChild(close);
  251.     XtAddCallback(close, XmNactivateCallback, PopdownTextOptions,
  252.           (XtPointer)text_options);
  253.  
  254.     apply = XmCreatePushButtonGadget(textOptions, "apply", NULL, 0);
  255.     XtManageChild(apply);
  256.     XtAddCallback(apply, XmNactivateCallback, Apply, (XtPointer)text_options);
  257.     
  258. #else
  259.     popup = XtCreatePopupShell(
  260.             "textOptionsShell", transientShellWidgetClass, 
  261.             parent,
  262.             NULL, 0 );
  263.     shell = popup;
  264.     
  265.     textOptions = XtCreateManagedWidget( 
  266.             "textOptions", formWidgetClass, 
  267.             popup,
  268.             NULL, 0 );
  269.  
  270.     title = XtCreateManagedWidget(
  271.             "title", labelWidgetClass, 
  272.             textOptions,
  273.             NULL, 0 );
  274.  
  275.     status = XtCreateManagedWidget(
  276.             "fontStatus", labelWidgetClass, 
  277.             textOptions,
  278.             NULL, 0 );
  279.  
  280.     font_label = XtCreateManagedWidget(
  281.             "fontLabel", labelWidgetClass, 
  282.             textOptions,
  283.             NULL, 0 );
  284.  
  285.     font = XtCreateManagedWidget(
  286.             "fontName", asciiTextWidgetClass, 
  287.             textOptions,
  288.             NULL, 0 );
  289.  
  290.     text_label = XtCreateManagedWidget(
  291.             "textLabel", labelWidgetClass, 
  292.             textOptions,
  293.             NULL, 0 );
  294.  
  295.     text = XtCreateManagedWidget(
  296.             "textString", asciiTextWidgetClass, 
  297.             textOptions,
  298.             NULL, 0 );
  299.  
  300.     close = XtCreateManagedWidget(
  301.             "close", commandWidgetClass, 
  302.             textOptions,
  303.             NULL, 0 );
  304.     XtAddCallback(close, XtNcallback, PopdownTextOptions,
  305.           (XtPointer)text_options);
  306.  
  307.     apply = XtCreateManagedWidget(
  308.             "apply", commandWidgetClass, 
  309.             textOptions,
  310.             NULL, 0 );
  311.     XtAddCallback(apply, XtNcallback, Apply, (XtPointer) text_options);
  312. #endif
  313.  
  314.     text_options->up     = False;
  315.     text_options->popup  = popup;
  316.     text_options->shell  = shell;
  317.     text_options->text   = text;
  318.     text_options->font   = font;
  319.     text_options->status = status;
  320.  
  321. #ifndef USE_ATHENA
  322.     text_options->font_name = (String)XmTextFieldGetString(text_options->font);
  323. #else
  324.     XtVaGetValues( text_options->font, 
  325.                    XtNstring, &font_name, 
  326.                    NULL );
  327.     text_options->font_name = XtNewString( font_name );
  328. #endif
  329.     text_options->font_struct = XLoadQueryFont(XtDisplay(parent),
  330.                            text_options->font_name);
  331.  
  332. #ifndef USE_ATHENA
  333.     text_options->text_string=(String)XmTextFieldGetString(text_options->text);
  334. #else
  335.     XtVaGetValues( text_options->text, 
  336.                    XtNstring, &text_string, 
  337.                    NULL );
  338.     text_options->text_string = XtNewString(text_string);
  339. #endif
  340.  
  341.     
  342.     return text_options;
  343. }
  344.  
  345.